package com.lagou.rpc.consumer.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class TestRoundRobin {

    // 1.定义map, key-ip,value-weight
    static Map<String,Integer> ipMap = new HashMap<>();

    static {
        ipMap.put("127.0.0.1:9001", 1);
        ipMap.put("127.0.0.1:9002", 1);
//        ipMap.put("127.0.0.1:8083", 1);
    }

    static Integer pos = 0;

    /**
     * 轮询算法
     * @return
     */
    public String RoundRobin(){
        Map<String,Integer> ipServerMap = new ConcurrentHashMap<>();
        ipServerMap.putAll(ipMap);
        // 2.取出来key,放到set中
        Set<String> ipset = ipServerMap.keySet();
        // 3.set放到list，要循环list取出
        ArrayList<String> iplist = new ArrayList<String>();
        iplist.addAll(ipset);
        String serverName = null;
        // 4.定义一个循环的值，如果大于set就从0开始
        synchronized(pos){
            if (pos >= ipset.size()){
                pos=0;
            }
            serverName = iplist.get(pos);
            //轮询+1
            pos ++;
        }
        return serverName;

    }

    public static void main(String[] args) {
        TestRoundRobin testRoundRobin=new TestRoundRobin();
        for (int i=0;i<10;i++){
            String serverIp=testRoundRobin.RoundRobin();
            System.out.println(serverIp);
        }
    }

}
